package server;
//*     "/rmmsg msgid": Remove the message with msgid from the buffer, 
//      and then broadcast to others the following message. 
//      Student1> /rmmsg 101 
//      Student1 removes the msg '101'. 

import java.net.*;
import java.io.*;
import java.util.*;


class who extends ChatFormat
{
	public who(String line)
	{
		super(line);
	}
	
	
	public void display_online_users(HashMap<String, KKMultiServerThread.ChatIO> chat_map,PrintWriter out,KKMultiServerThread server)
	{
			Set set = chat_map.entrySet();
			Iterator It = set.iterator();
			
			server.send_reply_client(out, "Name			IP/port" );
			while (It.hasNext()) {
				Map.Entry me = (Map.Entry)It.next();
				Socket s = ( (KKMultiServerThread.ChatIO) me.getValue() ).socket;
				
				String client_name = server.getUser();
				
				String name = (String) me.getKey();
				// /show Student0: 140.113.210.62/3145 
				if(name.compareTo(client_name) == 0)
				server.send_reply_client(out, name + ": " + s.getInetAddress().getHostAddress()  + "/" + s.getPort() + " <-- myself");
				else
				server.send_reply_client(out, name + ": " + s.getInetAddress().getHostAddress()  + "/" + s.getPort() );
				
				
		}
	}

	public void execute(KKMultiServerThread server)
	{
		//int new_id = server.add_msg_queue(msg);
		
		Set set = KKMultiServerThread.getChatMap().entrySet();
		Iterator It = set.iterator();
		
		int n=0;
		while (It.hasNext()) {
			Map.Entry me = (Map.Entry)It.next();
			PrintWriter out = ( (KKMultiServerThread.ChatIO) me.getValue() ).out;
			//String name = server.getUser();
			//Student1 removes the msg '101'. 
			//out.println(name + " removes the msg '" + msg + "'.");
			//server.send_reply_client(out, name + " removes the msg '" + msg + "'." );
			display_online_users(KKMultiServerThread.getChatMap(),out,server);
		}

	}
}
